package com.shiroha.notification.controller;

import com.shiroha.common.event.ServiceEventType;
import com.shiroha.common.utils.Result;
import com.shiroha.common.enums.NotificationType;
import com.shiroha.notification.pojo.Notification;
import com.shiroha.notification.service.INotificationService;
import lombok.AllArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/ns/notifications")
@AllArgsConstructor
public class NotificationController {

    private final INotificationService notificationService;

    // 发送通知
    @PostMapping
    public Result<String> sendNotification(@RequestParam("type") int type, @RequestBody Map<String, Object> payload) {
        ServiceEventType eventType = ServiceEventType.fromValue(type);
        return notificationService.publishNotification(eventType, payload) ?
                Result.success("通知发布成功") : Result.error("通知发布失败");
    }

    // 获取通知列表
    @GetMapping
    public List<Notification> getNotifications(@AuthenticationPrincipal Jwt jwt) {
        return notificationService.getAllNotificationsByUserId(UUID.fromString(jwt.getSubject()));
    }

    // 已读通知
    @PutMapping("/{notificationId}")
    public Result<String> readNotification(@AuthenticationPrincipal Jwt jwt, @PathVariable Long notificationId) {
        return notificationService.readNotification(UUID.fromString(jwt.getSubject()), notificationId) ?
                Result.success("已读成功") : Result.error("已读失败");
    }
}
